package com.ltsznh.android.familybooks.category;

import android.app.DialogFragment;
import android.content.ContentValues;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ltsznh.android.familybooks.R;
import com.ltsznh.android.familybooks.db.dbo;

/**
 * Created by Administrator on 13-9-18.
 */
public class Category2AddDialogFragment extends DialogFragment {

    private Context ctx = null;
    private String tableName = "";
    private String column1;
    private String column2;
    private String column3;
    private String invType;

    static Category2AddDialogFragment newInstance(String addText) {
        Category2AddDialogFragment fragment = new Category2AddDialogFragment();
        Bundle args = new Bundle();
        args.putString("addText", addText);
        fragment.setArguments(args);
//        fragment.getView().set.setTitle("test");
        return fragment;
    }

    public void setArgs(Context context, String tableName,String invType,String column1, String column2,String column3){
        ctx = context;
        this.tableName = tableName;
        this.invType = invType;
        this.column1 = column1;
        this.column2 = column2;
        this.column3 = column3;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int style = DialogFragment.STYLE_NORMAL, theme = android.R.style.Theme_Holo_Light_Dialog;
        setStyle(style, theme);
    }




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // 填充对话框的UI
        final View view = inflater.inflate(R.layout.edit_dialog, container, false);
        //对话框标题
        getDialog().setTitle(R.string.title_activity_add);
        // 更新对话框的内容
        final TextView addTextView = (TextView)view.findViewById(R.id.textView_edit_dialog_text);
        addTextView.setText(getArguments().getString("addText") + " > ");

        final EditText addEditText = (EditText)view.findViewById(R.id.editText_edit_dialog_text);

        Button button = (Button)view.findViewById(R.id.button_edit_dialog_button);

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // When button is clicked, call up to owning activity.
                dbo.getWritableDatabase(ctx);
                ContentValues values = new ContentValues();
                values.put(column1,invType);
                values.put(column2,getArguments().getString("addText"));
                values.put(column3,addEditText.getText().toString());

                long i = dbo.insertData(ctx, tableName, null, values);
                if(i >= 0){
                    Toast.makeText(ctx,getResources().getString(R.string.text_add_success),Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(ctx,"Error",Toast.LENGTH_SHORT).show();
                }
                dbo.closeDatabase();
                ((Category2Activity)getActivity()).refreshData();
                dismiss();
            }
        });

        return view;
    }
}
